﻿Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.ComponentModel

<Serializable()> _
Public Class SortableCollection(Of T)
    Inherits BindingList(Of T)

    Private _isSorted As Boolean

    Protected Overrides Sub ApplySortCore(ByVal [property] As PropertyDescriptor, ByVal direction As ListSortDirection)
        Dim items As List(Of T) = TryCast(MyBase.Items, List(Of T))
        If (Not items Is Nothing) Then
            Dim pc As New PropertyComparer(Of T)([property], direction)
            items.Sort(pc)
            Me._isSorted = True
        Else
            Me._isSorted = False
        End If
        Me.OnListChanged(New ListChangedEventArgs(ListChangedType.Reset, -1))
    End Sub

    Protected Overrides Sub RemoveSortCore()
        Me._isSorted = False
    End Sub

    ' Properties
    Protected Overrides ReadOnly Property IsSortedCore() As Boolean
        Get
            Return Me._isSorted
        End Get
    End Property

    Protected Overrides ReadOnly Property SupportsSortingCore() As Boolean
        Get
            Return True
        End Get
    End Property


End Class
